{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1、SVM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "accuracy is : 0.8625\n",
      "混淆矩阵如下：\n",
      "[[  0  13   0]\n",
      " [  1 261  15]\n",
      " [  0  15  15]]\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAR4AAADzCAYAAABQZHk+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAdn0lEQVR4nO3de7QU5bnn8e+Puwp6JICDbshWY5RLIhJEEx2iuSgyLjU3D8Sc6IHRXCAm0SRLc5ITL+M5zpkxlwkxiSaOTBLxkKVGT2KQLCOjODEIijeM8QIRhIAQE0FQZPPMH1UN7bZ379rQXdXd/D6sXl391ltVT+1mP/utt96qUkRgZpanXkUHYGZ7HyceM8udE4+Z5c6Jx8xy58RjZrlz4jGz3PUpOgAz23OTNTk2sCFT3aUsvSsiJtc5pKqceMxawAY28KCWZKrbKzSkzuF0y4nHrEWEslasaxiZOPGYtYjMiacBOPGYtYAQ7Mh6qqijrqFk4sRj1iKaqcXj0+lVSGqXtFXSMkkDJC2W9IikJyRd3s2yH5UUkiaknw9P17M5n+h7pnxf088rJT2Wxtxlr6WksyUtT38mN6VlDb2v8Kbv9sj0vfR6WdIXKiwzUtI9kh6W9KikKWl5Q+xvKNurO5JGpPv5ZPq9fj4tv0zSC2U/pylly1wq6RlJT0k6tftgI/zq4gW0A4+n0wIGptN9gd8Dx3ex3CDgXuABYEKneZuL3q/u9jX9vBIY0s0yRwAPAwemn4c1w75W2t+y8t7An4G3Vph3HfCZdHo0sLJR9ne83hVbB0SmF7Ckm5/NcGB8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Y2vyRaJWB/tIWpZO3wf8mOQQaHFErEjLTwHeWeq/AQ4AjgAmAXMjogNYI+m3FdZ/PHBvaV0R8Zcu4vgAMDp90BvA/ukTJycBH06X/ZWklzLs04WSPpROj0hj3QjsAP49Lf8pcKukgen+/rxs2/0zbGOv0iitmSyceJrD1ogYV16Q/gK+Ul4EfC4i7upUbwpJF0A1ylAHkkPzd0fE1gqxZFm+VP8kkiT27ojYImkhMKCL6pFu96+dfwa2S7NdJNpEjTPrxl3AZyT1BZD09vS51/cCU9M+oOHAyRWW/R3wXkmHpssOTss3AYPK6i0gOewhrVdKBPcC56RlpwEHdhPrAcBLadI5iqTFVdILKLXaPk5yCPcysELSx9JtSNKbnnS5V8vYv9MoycmJp3X8iKT/5iFJjwM/JGnR3gY8DTwGfB/4v50XjIgXSfplbpX0CLsOdf4D+FCpcxm4EJiQdl4vZ9fZtcuBSZIeIjnke76bWOcDfSQ9ClwJPFA27xVgjKSlJH04V6Tl5wAz0vieAM7M8DPZqzRT4lFE5haymTWo3uMnxD73LclU95WBWlr0tWfu4zFrEY3SmsnCicesBYRPp5tZEdziMbPcOfGYWe6aKfE00VGhmXWllheJptftPSXpGUmX1CNeJx6zVlCjAYTpnQ6+R3JN4GhgWrVbp+wuJx6zFlGjFs9E4JmIeC4itgE3U4fBmu7jMWsRNTqdfgiwquzzauC4mqy5jBOPWStYuvQuurm7Y5kBksqHOV9XdpfFSm2iml/e4MRj1gIiYnKNVrWa5DYlJW3Amhqteyf38ZhZuQeBIyQdKqkfMBW4o9YbcYvHzHaKiO2SZpHcZqU3cENEPFHr7fjqdDPLnQ+1zCx3TjxmljsnHjPLnROPmeXOicfMcufEY2a5c+Ixs9w58ZhZ7v4/dph0OsROP+oAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 288x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD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lnWAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn import svm\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "import warnings\n",
    "\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "df=pd.read_csv('D3.csv')\n",
    "\n",
    "\"\"\"特征压缩成3类,分别是 1: [3,4],2: [5,6],3: [7,8].\"\"\"\n",
    "df['quality'][df['quality']<=4]=1\n",
    "df['quality'][df['quality']>=7]=3\n",
    "df['quality'][df['quality']==5]=2\n",
    "df['quality'][df['quality']==6]=2\n",
    "X=df.iloc[:,:-1]\n",
    "y=df.iloc[:,-1]\n",
    "np.random.seed(0)\n",
    "X_train, X_test, y_train, y_test = train_test_split( X,y, test_size=0.2)\n",
    "classifier = svm.SVC(C=30,gamma=0.01)\n",
    "classifier.fit(X_train, y_train)\n",
    "predicted = classifier.predict(X_test)\n",
    "wrong_numbers=len(np.where(predicted!=y_test)[0])\n",
    "accuracy=1-wrong_numbers/len(y_test)\n",
    "print(\"accuracy is :\",accuracy)\n",
    "\n",
    "\"\"\"绘制混淆矩阵\"\"\"\n",
    "from sklearn.metrics import confusion_matrix\n",
    "import matplotlib.pyplot as plt\n",
    "y_pred = np.array(predicted)\n",
    "y_true = np.array(y_test)\n",
    "C = confusion_matrix(y_true, y_pred)\n",
    "print(\"混淆矩阵如下：\")\n",
    "print(C)\n",
    "plt.matshow(C,cmap=plt.cm.cool)\n",
    "plt.colorbar()\n",
    "for i in range(len(C)):\n",
    "    for j in range(len(C)):\n",
    "        plt.annotate(C[j, i], xy=(i, j), horizontalalignment='center', verticalalignment='center')\n",
    "plt.ylabel('True label')\n",
    "plt.xlabel('Predicted label')\n",
    "label_name=['[3,4]','[5,6]','[7,8]']\n",
    "plt.xticks(np.array(range(len(label_name))),label_name )  # 将标签印在x轴坐标上\n",
    "plt.yticks(np.array(range(len(label_name))), label_name)\n",
    "plt.show()\n",
    "from sklearn.metrics import accuracy_score,confusion_matrix,precision_score,recall_score,f1_score\n",
    "\n",
    "accuracy=accuracy_score(y_true,y_pred)\n",
    "precision=precision_score(y_true,y_pred,average='weighted')\n",
    "recall=recall_score(y_true,y_pred,average='weighted')\n",
    "F1=f1_score(y_true,y_pred,average='weighted')\n",
    "plt.bar(['accuracy','precision','recall','F1'],\n",
    "        [accuracy,precision,recall,F1],\n",
    "        color=['blue','red','orange','pink'])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2、逻辑回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "accuracy is: 0.9014084507042254\n",
      "混淆矩阵如下：\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQoAAADuCAYAAADBaUnpAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAU10lEQVR4nO3dfbRVdZ3H8fcHREQRlQBDiMEm8wkV8ZaYDYOWhmn2sMwkbWyqRU1OZuWkzZqVo60p1oxND5M9mDpqGiYJmVr4bGhRei8iIWZMYoqggGIqoPLwnT/OPngg7tn7XPY5Z59zPq+1zuLsh7P3lwP3e/f+7d/v91VEYGZWTb9mB2BmxedEYWapnCjMLJUThZmlcqIws1ROFGaWyonCrI1J+pykhyUtkjRD0i6Shkq6XdKS5M+9Uo/jfhRmxTFFU2I1qzPt20PPrRExpbftkkYB9wEHRcR6SdcDvwAOAp6LiOmSzgf2iojzqp1rp8x/AzOru9Ws5gF1Z9q3X2hYht12AgZJ2gDsCiwHvgRMTrZfBdwDVE0Uv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l4f7kxEzgb6EoaSxfz2tOXC4FJkuZTugV6IiXWOcBOkhYCXwF+W7FtLXCwpB5KbRAXJetPBz6exPcwpZ6CVqGIiUIRma80zazO+k/oikH3dqfvCKwdrJ5GdbhyG4VZwRSxjcKJwqxAwnNmmlkWvqIws1ROFGaWyonCzKoqaocrJwqzIvHkumaWhROFmaUq4uNR98w0KxBJcyiVJMhidURMqWc8ZU4UZpaqgBc5ZlY0ThRmlsqJwsxSOVGYWSonCjNL9f8C0QZbGNTcEgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 288x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD6CAYAAACxrrxPAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAQCElEQVR4nO3de4xcZ33G8e+DTUpowtUGgR3XVnECFpBAFxMEJUFcmiAhC0FLHGhwIHKjJtCqRSVSC6WiLdeiilywXOoEWiCI1AU3MkkopaSEuLVDrk4wXSUQL0bFgYiKcIkcfv1jzpbJZC9je+z1vv5+pJXPed93z/zm3fWzZ9+ZczZVhSRp/nvUXBcgSRoNA12SGmGgS1IjDHRJaoSBLkmNMNAlqRGzBnqSTUm+n+SOafqT5KNJxpPcluT5oy9TkjSbhUOMuQK4BPjkNP1nAiu7jxcCH+v+ndGiRYtq+fLlQxUpSeq56aab7quqxVP1zRroVXV9kuUzDFkDfLJ6VyhtS/KEJE+rqu/NdNzly5ezY8eO2R5ektQnyXem6xvFGvoSYHff/kTXJkk6jEYR6Jmibcr7CSRZn2RHkh179+4dwUNLkiaNItAngBP69pcCe6YaWFUbq2qsqsYWL55yCUiSdIBGEehbgHO6d7ucCvxotvVzSdLozfq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g+GI3UNfrD2yf3J5x7gS1V1SvexqqreyvTP+5cHqno/cB5wLLCtOwvvN9NNQ37et/0Q8/sEpX8u39c3l8+oqr9n+rm8GLik+63y93jk96eOIs0HOr2z5vur6iddWJwK/ApwWpIVAH2/vl4HXDj5iX1LLv+T5FlJHgXM9Ova44Hvdtvr+tqvA85PsrD/8boX//YAfwZccaBP8DBYluRF3fZaer/m99sGvDjJMwC6NfYTgW8CT0/ygq79+Mk5mJTk16vq9qr6AL0XSgcD/Xrgjd3YE4FlwHy9qdswrgXe0vcaxJIkTwG+DPxOkid37ZPfs/3fc28+3MXqyHI0BPo1wMIkt9E7g94G7KW37LI5ya38cungL4EnJrmja39Z134RcDXwb8BM96j5IPC+JDfQu03CpI8D9wK3dcc9u6/vU/SWNO48iOd4qN0FvLmbwycx8AdMutcA1gGf6cZsA57Z3T//DcDF3fP+Eo88g/zDvvn+KfDFgf7LgAVJbqf3dVpXVT+nUVV1HfBp4MbuOV8FHF9VO4G/Ar7azdVHuk95D/C5JP/B/L174JxI8m1687guyUQe+Yd75h2vFJ1j6b0H9ubu1+ojTpLlwNXdMpKkI9h8XnOc95LcRG/t1HclSDponqFLUiOOhjV0SToqGOiS1AgDXZIaYaBLUiMMdElqhIEuSY34P333GRBJ+9frAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "def sigmoid(x):\n",
    "    return 1/(1+pow(np.e,-x))\n",
    "class logistic_regress():\n",
    "    def __init__(self):\n",
    "        pass\n",
    "    def fit(self,X_train,y_train):\n",
    "        self.w=[0]*(len(X_train.columns)+1)\n",
    "        self.w=np.matrix(self.w).reshape(-1,1)\n",
    "    #def gradint(w):\n",
    "        epcho=20\n",
    "        alpha=0.03\n",
    "        for i in range(epcho):\n",
    "            Sum=0\n",
    "            for j in range(len(X_train)):\n",
    "                y=y_train.iloc[j]\n",
    "                X=X_train.iloc[j:j+1,:]\n",
    "                X=np.c_[[1],X]\n",
    "                X=np.matrix(X)\n",
    "                f=y*self.w.T.dot(X.T)\n",
    "                Sum+=(1-sigmoid(f.sum()))*y*X.T\n",
    "                self.w=self.w+alpha*Sum\n",
    "    def predict(self,X_test,y_test):\n",
    "        self.results=[]\n",
    "        for i in range(len(X_test)):\n",
    "            #y_=y_test.target[i]\n",
    "            X_=X_test.iloc[i:i+1,:]\n",
    "            X_=np.c_[[1],X_]\n",
    "            X_=np.matrix(X_)\n",
    "            f_=self.w.T.dot(X_.T)\n",
    "            if sigmoid(f_.sum())>0.5:\n",
    "                self.results.append(1)\n",
    "            else:\n",
    "                self.results.append(-1)\n",
    "        return self.results\n",
    "    def accuracy(self):\n",
    "        wrong_numbers=len(np.where(y_test!=self.results)[0])\n",
    "        accuracy=(len(X_test)-wrong_numbers)/len(X_test)\n",
    "        return accuracy\n",
    "\n",
    "\"\"\"训练并测试模型\"\"\"\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "df=pd.read_csv('D4.csv')\n",
    "df.iloc[:,:-1]=df.iloc[:,:-1]/pow(10,np.ceil(np.log10(np.max(np.max(df.iloc[:,:-1])))))\n",
    "X=df.iloc[:,:-1]\n",
    "y=df.iloc[:,-1]\n",
    "np.random.seed(0)\n",
    "X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25)\n",
    "# df.target[df.target=='M']=1\n",
    "# df.target[df.target=='B']=-1\n",
    "\n",
    "a=logistic_regress()\n",
    "a.fit(X_train,y_train)\n",
    "a.predict(X_test,y_test)\n",
    "results=a.results\n",
    "print(\"accuracy is:\",a.accuracy())\n",
    "\n",
    "\n",
    "\"\"\"绘制混淆矩阵\"\"\"\n",
    "from sklearn.metrics import confusion_matrix\n",
    "import matplotlib.pyplot as plt\n",
    "y_pred = results\n",
    "y_true = np.array(y_test)\n",
    "C = confusion_matrix(y_true, y_pred)\n",
    "print(\"混淆矩阵如下：\")\n",
    "plt.matshow(C,cmap=plt.cm.cool)\n",
    "plt.colorbar()\n",
    "for i in range(len(C)):\n",
    "    for j in range(len(C)):\n",
    "        plt.annotate(C[j, i], xy=(i, j), horizontalalignment='center', verticalalignment='center')\n",
    "plt.ylabel('True label')\n",
    "plt.xlabel('Predicted label')\n",
    "label_name=['-1','1']\n",
    "plt.xticks(np.array(range(len(label_name))),label_name )  # 将标签印在x轴坐标上\n",
    "plt.yticks(np.array(range(len(label_name))), label_name)\n",
    "plt.show()\n",
    "accuracy=a.accuracy()\n",
    "precision=C[0][0]/(C[0][0]+C[1][0])\n",
    "recall=C[0][0]/C[0].sum()\n",
    "F1=(2*precision*recall)/(precision+recall)\n",
    "plt.bar(['accuracy','precision','recall','F1'],\n",
    "        [accuracy,precision,recall,F1],\n",
    "        color=['blue','red','orange','pink'])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3、朴素贝叶斯"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7225\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'绘制混淆矩阵'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class naive_bayes():\n",
    "    def __init__(self):\n",
    "        pass\n",
    "\n",
    "    def fit(self, X_train, y_train):\n",
    "        self.data = X_train\n",
    "        self.target = y_train\n",
    "\n",
    "    def predict(self, X_test, y_test):\n",
    "        self.data = pd.concat([X_train, X_test])\n",
    "        self.target = pd.concat([y_train, y_test])\n",
    "        self.df = pd.concat([self.data, self.target], axis=1)\n",
    "        target_set = dict(self.target.value_counts())\n",
    "        prob_set = []\n",
    "        prob_class_set = []\n",
    "        for i in range(len(X_test)):\n",
    "            prob_set.append({})\n",
    "            for cls in target_set.keys():\n",
    "                prob_class = (target_set[cls] + 1) / (sum(target_set.values()) + 1)\n",
    "                problity = 1\n",
    "                for j in self.data.columns:\n",
    "                    prob_x = (len(self.df[self.df[j] == X_test[j].iloc[i]][self.target == cls]) + 1) / (\n",
    "                                target_set[cls] + 1)\n",
    "                    problity = problity * prob_x\n",
    "                    problity = problity * prob_class\n",
    "                prob_set[i][cls] = problity\n",
    "        for i in prob_set:\n",
    "            for key, value in i.items():\n",
    "                if value == max(i.values()):\n",
    "                    prob_class_set.append(key)\n",
    "        return prob_class_set\n",
    "        # print(y_test)\n",
    "\n",
    "    def accuracy(self):\n",
    "        prob_class_set = self.predict(X_test, y_test)\n",
    "        true_numbers = len(X_test) - len(np.where(prob_class_set != y_test)[0])\n",
    "        return (true_numbers) / len(X_test)\n",
    "\n",
    "\n",
    "\"\"\"训练并测试模型\"\"\"\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "import warnings\n",
    "\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "df = pd.read_csv('D3.csv')\n",
    "X = df.iloc[:,:-1]\n",
    "y = df.iloc[:,-1]\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25,random_state=114514)\n",
    "a = naive_bayes()\n",
    "a.fit(X_train, y_train)\n",
    "results=a.predict(X_test, y_test)\n",
    "print(a.accuracy())\n",
    "\n",
    "\"\"\"绘制混淆矩阵\"\"\"\n",
    "# from sklearn.metrics import confusion_matrix\n",
    "# import matplotlib.pyplot as plt\n",
    "# y_pred = results\n",
    "# y_true = np.array(y_test)\n",
    "# C = confusion_matrix(y_true, y_pred)\n",
    "# print(\"混淆矩阵如下：\")\n",
    "# print(C)\n",
    "# plt.matshow(C,cmap=plt.cm.cool)\n",
    "# plt.colorbar()\n",
    "# for i in range(len(C)):\n",
    "#     for j in range(len(C)):\n",
    "#         plt.annotate(C[j, i], xy=(i, j), horizontalalignment='center', verticalalignment='center')\n",
    "# plt.ylabel('True label')\n",
    "# plt.xlabel('Predicted label')\n",
    "# label_name=['1','2','3']\n",
    "# plt.xticks(np.array(range(len(label_name))),label_name )  # 将标签印在x轴坐标上\n",
    "# plt.yticks(np.array(range(len(label_name))), label_name)\n",
    "# plt.show()\n",
    "# from sklearn.metrics import accuracy_score,confusion_matrix,precision_score,recall_score,f1_score\n",
    "\n",
    "# accuracy=accuracy_score(y_true,y_pred)\n",
    "# precision=precision_score(y_true,y_pred,average='weighted')\n",
    "# recall=recall_score(y_true,y_pred,average='weighted')\n",
    "# F1=f1_score(y_true,y_pred,average='weighted')\n",
    "# plt.bar(['accuracy','precision','recall','F1'],\n",
    "#         [accuracy,precision,recall,F1],\n",
    "#         color=['blue','red','orange','pink'])\n",
    "# plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4、神经网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     sepal_length  sepal_width  petal_length  petal_width\n",
      "0             4.9          3.0           1.4          0.2\n",
      "1             4.7          3.2           1.3          0.2\n",
      "2             4.6          3.1           1.5          0.2\n",
      "3             5.0          3.6           1.4          0.2\n",
      "4             5.4          3.9           1.7          0.4\n",
      "..            ...          ...           ...          ...\n",
      "144           6.7          3.0           5.2          2.3\n",
      "145           6.3          2.5           5.0          1.9\n",
      "146           6.5          3.0           5.2          2.0\n",
      "147           6.2          3.4           5.4          2.3\n",
      "148           5.9          3.0           5.1          1.8\n",
      "\n",
      "[149 rows x 4 columns]\n",
      "accuracy is 0.8947368421052632\n",
      "混淆矩阵如下：\n",
      "[[10  0  0]\n",
      " [ 0 12  4]\n",
      " [ 0  0 12]]\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQYAAAD3CAYAAAD/jPo0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIs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ilwRVrfOuCCJv5MKqOMwaCIps8izSxjo06ZGQf/ZMXwKwJvHKKVnRr74TYGs4KVsY3BwWBWoPDtSjNrxGcMZlbHwWBmdcoYDCW8ujGrjqwHUaXjZp6T9AtJN7Rbl4PBrEgZdnBKR+r+NcmYnBOBS4Yamj8UB4NZwTI8Y/gI8IuI+GVE7Abuoc3OZG5jMCtYhrcrJwKbB3zeApzWzoYcDGZFWrnyBwwzM9YAB0ka2E1y8T6zVDU6r2ira7ODwaxAETEvw81tIRkG368XeKGdDbmNwWzkeBr4oKRjJB0ILAAebGdDPmMwGyEiYo+ka0iG8Y8Cbo+Ide1sy6MrzayOLyXMrI6DwczqOBjMrI6DwczqOBjMrI6DwczqOBjMrI6Dwczq/H+vsBUjn3dLjwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 288x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAPQklEQVR4nO3dfZDdVX3H8ffHTalaEAVWpybYZGoAMyrYrhFHKzgigh2HcbSVB4tRGcpUtJ3WKcy0OHZs62OdjjyYSS1gWy2ONNWUiYClViuaNos8GTA2A0pinLooY0d8YILf/nF/W683m90bcpObPXm/ZnZyf+ec/d3vPXv3k7Nn7/1tqgpJ0uL3uHEXIEkaDQNdkhphoEtSIwx0SWqEgS5JjVgyrjs+5phjavny5eO6e0lalG677bYHq2pyrr6xBfry5cuZnp4e191L0qKU5Jt76nPLRZIaYaBLUiMMdElqhIEuSY0w0CWpEQa6JDXCQJekRhjoktQIA12SGjG2d4pqzJJxVzBe/mEXNcgVuiQ1wk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gD0/fh6M3Dx7Cf2bbn8T5JnJXkcMN+Pa0cC3+pur+lrvxm4KMmS/vvrfvm3E/hT4NrH+gAPgGckeWF3+xx6P+b32wS8KMkzAbo99uOArwFPT/L8rv2I2TmYleRXq+ruqnovvV+UDgb6F4DzurHHAc8AFutVOodxE/Cmvt9BLE3yVOAW4LeTHN21zz5n+59zbzjQxergcigE+o3AkiR30VtBbwJm6G27rE9yJz/bOvhz4ClJvtq1v7RrvxS4Afg3Bi4JPOB9wLuT3ErvMgmzPgI8ANzVnffcvr6P0dvSuGcfHuP+di/whm4Oj2LgD5h0vwNYA/xjN2YTcEJ3/fzXAZd3j/uz7L6C/IO++f4R8JmB/quAiSR30/s6ramqn9CoqroZ+Djw5e4xXw8cUVVbgL8APt/N1Qe7T3kn8Mkk/8HivRzsWCT5Br15XJNkR3b/wz2Ljm/9H7P0XgN7e/dj9UEnyXLghm4bSdJBbDHvOS56SW6jt3fqqxIk7TNX6JLUiENhD12SDgkGuiQ1wkCXpEYY6JLUCANdkhrxf5lI0f8M3XyBAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "def sigmoid(x):\n",
    "    return 1 / (1 + np.exp(-x))\n",
    "\n",
    "\"\"\"定义神经元\"\"\"\n",
    "class Neuron:\n",
    "    def __init__(self, weights, bias):\n",
    "        self.weights = weights\n",
    "        self.bias = bias\n",
    "\n",
    "    def feedforward(self, inputs):\n",
    "        # 加权输入，加入偏置，然后使用激活函数\n",
    "        total = np.dot(self.weights, inputs) + self.bias\n",
    "        return sigmoid(total)\n",
    "\n",
    "\"\"\"定义一个隐藏层神经元数目为4，输出层神经元为1的网络\"\"\"\n",
    "class NeuralNetwork:\n",
    "    def __init__(self,alpha=0.9,epoch=100):\n",
    "        self.alpha = alpha\n",
    "        self.epoch=epoch\n",
    "\n",
    "    def fit(self, X_train, y_train):\n",
    "        # self.initial()\n",
    "        self.h1_weight = np.array([0.1] * len(X_train.columns))\n",
    "        self.h1_bias = 0\n",
    "        self.h2_weight = np.array([0] * len(X_train.columns))\n",
    "        self.h2_bias = 0\n",
    "        self.h3_weight = np.array([0] * len(X_train.columns))\n",
    "        self.h3_bias = 0\n",
    "        self.h4_weight = np.array([0] * len(X_train.columns))\n",
    "        self.h4_bias = 0\n",
    "        self.o1_weight = np.array([0, 0, 0, 0])\n",
    "        self.o1_bias = 0\n",
    "        \n",
    "        self.update_network()\n",
    "        self.input_x = X_train\n",
    "        self.x = 0\n",
    "        self.target = y_train\n",
    "        for epoch in range(self.epoch):\n",
    "            for i in range(len(X_train)):\n",
    "                self.x = X_train.iloc[i:i + 1, :].sum()\n",
    "                self.x = np.array(self.x)\n",
    "                self.target = y_train.iloc[i]\n",
    "                self.updated_weight()\n",
    "                self.updated_bias()\n",
    "                self.update_network()\n",
    "            # print(self.h1_weight,self.h2_weight,self.o1_weight)\n",
    "            # print(self.h1_bias,self.h2_bias,self.o1_bias)\n",
    "\n",
    "    def update_network(self):\n",
    "        self.h1 = Neuron(self.h1_weight, self.h1_bias)\n",
    "        self.h2 = Neuron(self.h2_weight, self.h2_bias)\n",
    "        self.h3 = Neuron(self.h3_weight, self.h3_bias)\n",
    "        self.h4 = Neuron(self.h4_weight, self.h4_bias)\n",
    "        self.o1 = Neuron(self.o1_weight, self.o1_bias)\n",
    "\n",
    "    def feedforward(self):\n",
    "        out_h1 = self.h1.feedforward(self.x)\n",
    "        out_h2 = self.h2.feedforward(self.x)\n",
    "        out_h3 = self.h3.feedforward(self.x)\n",
    "        out_h4 = self.h4.feedforward(self.x)\n",
    "        # o1的输入是h1和h2的输出\n",
    "        out_o1 = self.o1.feedforward(np.array([out_h1, out_h2, out_h3, out_h4]))\n",
    "        return out_h1, out_h2, out_h3, out_h4, out_o1\n",
    "\n",
    "    def out_err(self):\n",
    "        out_o1 = self.feedforward()[4]\n",
    "        err_o1 = out_o1 * (1 - out_o1) * (out_o1 - self.target)\n",
    "        return err_o1\n",
    "\n",
    "    def hidden_err(self):\n",
    "        out_h1, out_h2, out_h3, out_h4 = self.feedforward()[0:4]\n",
    "        # self.out_h2=self.feedforward(self.input_x)[1]\n",
    "        err_o1 = self.out_err()\n",
    "\n",
    "        Sum1 = 0\n",
    "        Sum1 += err_o1 * self.o1_weight[0]\n",
    "        err_h1 = out_h1 * (1 - out_h1) * (Sum1)\n",
    "\n",
    "        Sum2 = 0\n",
    "        Sum2 += err_o1 * self.o1_weight[1]\n",
    "        err_h2 = out_h2 * (1 - out_h2) * (Sum2)\n",
    "\n",
    "        Sum3 = 0\n",
    "        Sum3 += err_o1 * self.o1_weight[2]\n",
    "        err_h3 = out_h3 * (1 - out_h3) * (Sum3)\n",
    "\n",
    "        Sum4 = 0\n",
    "        Sum4 += err_o1 * self.o1_weight[3]\n",
    "        err_h4 = out_h4 * (1 - out_h4) * (Sum4)\n",
    "\n",
    "        return err_h1, err_h2, err_h3, err_h4\n",
    "    \n",
    "    \"\"\"更新每一次训练的权值\"\"\"\n",
    "    def updated_weight(self):\n",
    "        out_h1, out_h2, out_h3, out_h4 = self.feedforward()[0:4]\n",
    "        err_h1, err_h2, err_h3, err_h4 = self.hidden_err()\n",
    "        err_o1 = self.out_err()\n",
    "        self.h1_weight = self.h1_weight - self.alpha * (err_h1) * self.x\n",
    "        self.h2_weight = self.h2_weight - self.alpha * (err_h2) * self.x\n",
    "        self.h3_weight = self.h3_weight - self.alpha * (err_h3) * self.x\n",
    "        self.h4_weight = self.h4_weight - self.alpha * (err_h4) * self.x\n",
    "        self.o1_weight = self.o1_weight - self.alpha * (err_o1) * np.array([out_h1, out_h2, out_h3, out_h4])\n",
    "        # return self.h1_weight,self.h2_weight,self.h3_weight,self.h4_weight,self.o1_weight\n",
    "\n",
    "    \"\"\"更新每一次训练的偏置\"\"\"\n",
    "    def updated_bias(self):\n",
    "        err_h1, err_h2, err_h3, err_h4 = self.hidden_err()\n",
    "        err_o1 = self.out_err()\n",
    "        self.h1_bias = self.h1_bias - self.alpha * (err_h1)\n",
    "        self.h2_bias = self.h2_bias - self.alpha * (err_h2)\n",
    "        self.h3_bias = self.h3_bias - self.alpha * (err_h3)\n",
    "        self.h4_bias = self.h4_bias - self.alpha * (err_h4)\n",
    "        self.o1_bias = self.o1_bias - self.alpha * (err_o1)\n",
    "        # return self.h1_bias,self.h2_bias,self.h3_bias,self.h4_bias,self.o1_bias\n",
    "\n",
    "    def predict(self, X_test, y_test):\n",
    "        # self.update_network()\n",
    "        self.results = []\n",
    "        for i in range(len(X_test)):\n",
    "            self.x = X_test.iloc[i:i + 1, :].sum()\n",
    "            self.x = np.array(self.x)\n",
    "            result = self.feedforward()[4]\n",
    "            b = [0, 0.5, 1]\n",
    "            result = 0.5 * list(abs(np.array(b) - result)).index(abs(np.array(b) - result).min())\n",
    "            self.results.append(result)\n",
    "        return self.results\n",
    "    def accuracy(self):\n",
    "        wrong_numbers = len(np.where(self.results != y_test)[0])\n",
    "        return (len(X_test) - wrong_numbers) / len(X_test)\n",
    "\n",
    "\n",
    "\n",
    "\"\"\"模型的训练以及测试\"\"\"\n",
    "from sklearn.model_selection import train_test_split\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "df=pd.read_csv('D2.csv')\n",
    "df['class']=(df['class']-df['class'].min())/(df['class'].max()-df['class'].min())\n",
    "X=df.iloc[:,:-1]\n",
    "y=df.iloc[:,-1]\n",
    "\n",
    "X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25,random_state=114514)\n",
    "network = NeuralNetwork()\n",
    "network.fit(X_train, y_train)\n",
    "network.predict(X_test, y_test)\n",
    "results=network.results\n",
    "print(\"accuracy is\" ,network.accuracy())\n",
    "\n",
    "\"\"\"绘制混淆矩阵\"\"\"\n",
    "from sklearn.metrics import confusion_matrix\n",
    "import matplotlib.pyplot as plt\n",
    "y_pred = np.array(results)*2+1\n",
    "y_true = np.array(y_test)*2+1\n",
    "C = confusion_matrix(y_true, y_pred)\n",
    "print(\"混淆矩阵如下：\")\n",
    "print(C)\n",
    "plt.matshow(C,cmap=plt.cm.cool)\n",
    "plt.colorbar()\n",
    "for i in range(len(C)):\n",
    "    for j in range(len(C)):\n",
    "        plt.annotate(C[j, i], xy=(i, j), horizontalalignment='center', verticalalignment='center')\n",
    "plt.ylabel('True label')\n",
    "plt.xlabel('Predicted label')\n",
    "label_name=['1','2','3']\n",
    "plt.xticks(np.array(range(len(label_name))),label_name )  # 将标签印在x轴坐标上\n",
    "plt.yticks(np.array(range(len(label_name))), label_name)\n",
    "plt.show()\n",
    "from sklearn.metrics import accuracy_score,confusion_matrix,precision_score,recall_score,f1_score\n",
    "\n",
    "accuracy=accuracy_score(y_true,y_pred)\n",
    "precision=precision_score(y_true,y_pred,average='weighted')\n",
    "recall=recall_score(y_true,y_pred,average='weighted')\n",
    "F1=f1_score(y_true,y_pred,average='weighted')\n",
    "plt.bar(['accuracy','precision','recall','F1'],\n",
    "        [accuracy,precision,recall,F1],\n",
    "        color=['blue','red','orange','pink'])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5、knn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率: 0.8802816901408451\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from collections import Counter\n",
    "\n",
    "\n",
    "class knn:\n",
    "    def __init__(self, k):\n",
    "        self.k = k\n",
    "\n",
    "    def fit(self, X, y):\n",
    "        \"\"\"训练模型，实际上就是存储训练数据\"\"\"\n",
    "        self.X_train = X\n",
    "        self.y_train = y\n",
    "    \n",
    "    def distance(self,x1, x2):\n",
    "        \"\"\"计算欧几里得距离\"\"\"\n",
    "        return np.sqrt(np.sum(pow((x1 - x2),2)))\n",
    "\n",
    "    def predict(self, X):\n",
    "        \"\"\"预测新样本的类别\"\"\"\n",
    "        y_pred = [self._predict(x) for x in X]\n",
    "        return np.array(y_pred)\n",
    "\n",
    "    def _predict(self, x):\n",
    "        \"\"\"预测单个样本的类别\"\"\"\n",
    "        # 计算输入样本与所有训练样本的距离\n",
    "        distances = [self.distance(x, x_train) for x_train in self.X_train]\n",
    "        # 获取距离最近的k个样本的索引\n",
    "        k_indices = np.argsort(distances)[:self.k]\n",
    "        # 获取这k个样本的类别\n",
    "        k_nearest_labels = [self.y_train[i] for i in k_indices]\n",
    "        # 统计出现次数最多的类别\n",
    "        most_common = Counter(k_nearest_labels).most_common(1)\n",
    "        return most_common[0][0]\n",
    "\n",
    "    \n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "# # 加载鸢尾花数据集\n",
    "# iris = load_iris()\n",
    "# X = iris.data\n",
    "# y = iris.target\n",
    "\n",
    "# # 划分训练集和测试集\n",
    "# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "df=pd.read_csv('D4.csv')\n",
    "\n",
    "df.iloc[:,:-1]=df.iloc[:,:-1]/pow(10,np.ceil(np.log10(np.max(np.max(df.iloc[:,:-1])))))\n",
    "X=np.array(df.iloc[:,:-1])\n",
    "y=np.array(df.iloc[:,-1])\n",
    "np.random.seed(3)\n",
    "X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25)\n",
    "\n",
    "\n",
    "\n",
    "# 创建KNN分类器实例\n",
    "Knn = knn(2)\n",
    "\n",
    "# 训练模型\n",
    "Knn.fit(X_train, y_train)\n",
    "\n",
    "\n",
    "# 进行预测\n",
    "y_pred = Knn.predict(X_test)\n",
    "\n",
    "# 计算准确率\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(\"准确率:\", accuracy)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6、Adaboost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEICAYAAABPgw/pAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nO3de3wU9b3/8dcnmyskgSRIMARNQEKFYIQGQmwErPdaj/ZUaqVVbKuWny3tsdpTa9sjPlq1tbYeq7We2qNiK5XSWsXW02qtgUZDJEYjN8MlAQmXAOGSBJJAwvf3x0zSMeYGzZfJjJ/n48GDnZ3Z7372PZNPZr+72RVjDEoppYIvyu8ClFJKDQxt6EopFRLa0JVSKiS0oSulVEhoQ1dKqZDQhq6UUiGhDV19gIg0ichYC+NuEZELBnpcNTBEZKGI/OZkjyUiT4rIDwbifj/stKF/yIlIsYjc4L3OGJNojKn2q6bu6C8DNVBEJEtEjIhE+13LQNOGHmJhPGCVUj3Thn4cRGSMiDwrIntEpF5EHnavjxKR74rIVhHZLSJPicgwd13H2cAXRGSbiOwXkfkiMk1E3hGRAx3juNtfLyKvichDInJQRN4VkfM9678gIutFpFFEqkXky551s0WkVkS+JSK7gCdEJEVE/uTWvN+9nOlufzdwLvCwO83S8XiMiJzhXh7mPp497uP7rohEeWotEZH73bFrROTSPmKcJiLr3O2fEJF4T/2fFJG33UxeF5Gz3Ot/DZwGvODW+Z8iskhEbnXXj3ZrvtldPkNE9omI9Dauuy5DRP7gPr4aEfmaZ91CEfmd+/gbRWStiOT3cGyIiDzg7v+D7r7NFZEZIrJLRCKebT8lIu947mOpiPzGvY/VIpIjIt92x9omIhf1FKaI3C4im93brhORT3nW9bp/RCRbRJa7t30ZGNHL/fR4HPVnLPcx7nKzWSEik7rcxQgRedm9/XIRO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StxpiVInI/cBHwlnt9Is4nmuYBvzfG7IX3HeuZwBJxPhgvFqg5uWUH3gemXIImkFMuIjIbp6EWGmPycA74Srr5/BicM5vurvdeF99l3SHP5e8DrxpjcoHLPdv2NO4TwOeBa3B+MQRxDr7r4+pY7shFgJeNMWe7/yYaY75Ez5n8cyBjfgjcACQAK92zcK/ePrWo1XO5nWCfkPTGm/O9npzPMMb8Lz3n/BDwsPts9Mt88LhWIRfIho5z1rzfGHPYbQgzgDhglohkA3iehr4EfLXjhp4plzoROVNEooDenloNA7a7l6/3XP8SMF9Eor33577AtwP4LvDkiT5An50mIoXu5Wtwnsp7rQQ+JiJnALhz7DnAu0CGiExzr0/qyKeDiIwzxqw2xvwI54XSrg19BfA5d9sc4DRgUH0q50n0V+CLntcnRovISOAV4DMikuZe33Gse4/VeSe7WOW/oDb0vwDRIvIOzhn0SmAPzrTLsyJSyT+nB34ApIjIGvf689zrbwf+BPydLh/z28V9wL0i8hrORx90+BXwHvCOO+5cz7qncaYt1v0Lj9FP64F5br6pdPnCEvf1geuB37rbrAQ+4n5e/tXAQ24mL/PBs8T/8OyLZuD/uqx/BIiIyGqcfXi9MaaVDyFjzEvAYqDUzeP3QJIxZi1wN7DczfGn7k0WAktF5B/Y+MjXDykR2YKT8fUiUisf/IKfQUP/9N8Ccd6v+pb79DhQRCQL+JM7xaSUCpCwzkH6RkTexJkD1XcXKKVOKj1DV0qpkAjqHLpSSqkutKErpVRIaENXSqmQ0IaulFIhoQ1dKaVC4v8Dx5CIcG69kSAAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn.svm import SVC\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "df=pd.read_csv('D4.csv')\n",
    "X=df.iloc[:,0:-1]\n",
    "y=df.iloc[:,-1]#分类标签-1和1\n",
    "np.random.seed(3)\n",
    "x_train,x_test,y_train,y_test=train_test_split(X,y,test_size=0.25)#训练集：测试集=3：1\n",
    "\n",
    "def sign(x):#符号函数\n",
    "    for i in range(len(x)):\n",
    "        if x[i]>0:\n",
    "            x[i]=1\n",
    "        else:\n",
    "            x[i]=-1\n",
    "    return x\n",
    "#自己实现的adaboost，基分类器采用svm\n",
    "class Adaboost:\n",
    "    def __init__(self,T=50):\n",
    "        self.T=T#选定基分类器的个数为30\n",
    "    def fit(self,x_train,y_train):\n",
    "        self.x_train=np.array(x_train)\n",
    "        self.y_train=np.array(y_train)\n",
    "        self.train_weight=[1/len(x_train)]*len(x_train)#初始化每一训练集数据点的权重\n",
    "    def predict(self,x_test):\n",
    "        result=np.array([0]*len(x_test))\n",
    "        x_test=np.array(x_test)\n",
    "        for t in range(self.T):\n",
    "            err=0\n",
    "            Z=0\n",
    "            gamma=np.random.uniform(0,0.001)\n",
    "            a=SVC(gamma=gamma)#采用svm作为基分类器，并使每次得到的分类器不同\n",
    "            a.fit(self.x_train,self.y_train)\n",
    "            y_pred=a.predict(self.x_train)\n",
    "            I=y_pred-np.array(self.y_train)#统计预测值和真实值相异向量，相同为0，不通为1\n",
    "            I=I/2\n",
    "            I=abs(I)\n",
    "            for j in range(len(self.x_train)):#计算误分率\n",
    "                err=err+self.train_weight[j]*I[j]\n",
    "            if err>0.5:#小于随机预测就舍弃该若弱分类器\n",
    "                continue\n",
    "            alpha=0.5*np.log((1-err)/err)#计算该分类器的权重\n",
    "            for i in  range(len(self.train_weight)):\n",
    "                Z+=self.train_weight[i]*pow(np.e,-1*alpha*self.y_train[i]*y_pred[i])\n",
    "            for i in  range(len(self.train_weight)):#更新训练集各个数据点的权值\n",
    "                self.train_weight[i]=self.train_weight[i]*pow(np.e,-1*alpha*self.y_train[i]*y_pred[i])\n",
    "                self.train_weight[i]=self.train_weight[i]/Z#对权重归一化\n",
    "            result=result+alpha*np.array(a.predict(x_test))#每次得到的弱分类器\n",
    "        return (sign(result))#最终强分类模型\n",
    "d=SVC(gamma=0.001)\n",
    "d.fit(x_train,y_train)\n",
    "results_svm=d.predict(x_test)\n",
    "\n",
    "c=Adaboost()           \n",
    "c.fit(x_train,y_train)\n",
    "results_adaboost=c.predict(x_test)  \n",
    "\n",
    "#这是基分类器为svm的adaboost方法和svm各项分类指标对比\n",
    "import matplotlib.pyplot as plt\n",
    "y_true = np.array(y_test)\n",
    "from sklearn.metrics import accuracy_score,confusion_matrix,precision_score,recall_score,f1_score\n",
    "#下面是对两种分类模型分别计算分类指标\n",
    "accuracy_svm=accuracy_score(y_true,results_svm)\n",
    "precision_svm=precision_score(y_true,results_svm,average='weighted')\n",
    "recall_svm=recall_score(y_true,results_svm,average='weighted')\n",
    "F1_svm=f1_score(y_true,results_svm,average='weighted')\n",
    "\n",
    "\n",
    "accuracy_adaboost=accuracy_score(y_true,results_adaboost)\n",
    "precision_adaboost=precision_score(y_true,results_adaboost,average='weighted')\n",
    "recall_adaboost=recall_score(y_true,results_adaboost,average='weighted')\n",
    "F1_adaboost=f1_score(y_true,results_adaboost,average='weighted')\n",
    "\n",
    "#绘制柱状图\n",
    "labels=['accuracy','precision','recall','F1']\n",
    "x = np.arange(len(labels)) # x轴刻度标签位置\n",
    "width = 0.25 # 柱子的宽度\n",
    "# 计算每个柱子在x轴上的位置，保证x轴刻度标签居中\n",
    "# x - width/2，x + width/2即每组数据在x轴上的位置\n",
    "plt.bar(x - width/2+1,[accuracy_svm,precision_svm,recall_svm,F1_svm] ,\n",
    "        width, label='svm')\n",
    "plt.bar(x + width/2+1,[accuracy_adaboost,precision_adaboost,recall_adaboost,F1_adaboost] , \n",
    "        width, label='adaboost')\n",
    "plt.grid(linestyle='dashed')\n",
    "plt.xticks(x+1,labels=labels)\n",
    "plt.title('comparation between svm and adaboost')\n",
    "plt.legend(loc='upper left')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7、随机森林"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分类准确率: 0.956140350877193\n"
     ]
    }
   ],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "#数据集的处理\n",
    "df=pd.read_csv('D4.csv')\n",
    "df.iloc[:,:-1]=df.iloc[:,:-1]/pow(10,np.ceil(np.log10(np.max(np.max(df.iloc[:,:-1])))))\n",
    "X=df.iloc[:,:-1]\n",
    "y=df.iloc[:,-1]\n",
    "\n",
    "# 划分训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=114514)\n",
    "\n",
    "# 创建随机森林分类器实例\n",
    "# n_estimators 表示森林中树的数量，默认为1#0\n",
    "# random_state 用于固定随机种子，保证结果可复现\n",
    "rf_classifier = RandomForestClassifier(n_estimators=100, random_state=114514,criterion='entropy')#criterion='gini'\n",
    "#\n",
    "# 训练模型\n",
    "rf_classifier.fit(X_train, y_train)\n",
    "\n",
    "# 进行预测\n",
    "y_pred = rf_classifier.predict(X_test)\n",
    "\n",
    "# 计算准确率\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(f\"分类准确率: {accuracy}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 8、xgboost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分类准确率: 0.9736842105263158\n"
     ]
    }
   ],
   "source": [
    "import xgboost as xgb\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "#数据集的处理\n",
    "df=pd.read_csv('D4.csv')\n",
    "df.iloc[:,:-1]=df.iloc[:,:-1]/pow(10,np.ceil(np.log10(np.max(np.max(df.iloc[:,:-1])))))\n",
    "X=df.iloc[:,:-1]\n",
    "y=df.iloc[:,-1]\n",
    "\n",
    "\n",
    "# 划分训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=114514)\n",
    "\n",
    "# 创建XGBoost分类器\n",
    "xgb_classifier = xgb.XGBClassifier(objective='multi:softmax', num_class=2, random_state=114514,learning_rate=0.1)\n",
    "\n",
    "from sklearn.preprocessing import LabelEncoder \n",
    "le = LabelEncoder() \n",
    "y_train =le.fit_transform(y_train)\n",
    "\n",
    "# 训练模型\n",
    "xgb_classifier.fit(X_train, y_train)\n",
    "\n",
    "np.random.seed(0)\n",
    "# 进行预测\n",
    "y_pred = xgb_classifier.predict(X_test)\n",
    "y_pred=le.inverse_transform(y_pred)\n",
    "# 计算准确率\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(f\"分类准确率: {accuracy}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 9、梯度提升"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "scikit-learn梯度提升分类准确率: 0.9122807017543859\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "\n",
    "\n",
    "#数据集的处理\n",
    "df=pd.read_csv('D4.csv')\n",
    "df.iloc[:,:-1]=df.iloc[:,:-1]/pow(10,np.ceil(np.log10(np.max(np.max(df.iloc[:,:-1])))))\n",
    "X=df.iloc[:,:-1]\n",
    "y=df.iloc[:,-1]\n",
    "\n",
    "# 划分训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)\n",
    "\n",
    "# 创建梯度提升分类器\n",
    "gb_classifier = GradientBoostingClassifier(n_estimators=50, learning_rate=0.1, max_depth=3, random_state=42)\n",
    "# 训练模型\n",
    "gb_classifier.fit(X_train, y_train)\n",
    "# 进行预测\n",
    "y_pred = gb_classifier.predict(X_test)\n",
    "# 计算准确率\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(\"scikit-learn梯度提升分类准确率:\", accuracy)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 10、decision tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "决策树分类准确率: 0.9210526315789473\n"
     ]
    }
   ],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "# # 加载鸢尾花数据集，其中包含连续型特征\n",
    "# iris = load_iris()\n",
    "# X = iris.data  # 特征数据，包含连续型特征\n",
    "# y = iris.target  # 类别标签\n",
    "\n",
    "\n",
    "#数据集的处理\n",
    "df=pd.read_csv('D4.csv')\n",
    "df.iloc[:,:-1]=df.iloc[:,:-1]/pow(10,np.ceil(np.log10(np.max(np.max(df.iloc[:,:-1])))))\n",
    "X=df.iloc[:,:-1]\n",
    "y=df.iloc[:,-1]\n",
    "# 划分训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "# 创建决策树分类器\n",
    "dt_classifier = DecisionTreeClassifier(random_state=42)\n",
    "\n",
    "# 训练模型\n",
    "dt_classifier.fit(X_train, y_train)\n",
    "\n",
    "# 进行预测\n",
    "y_pred = dt_classifier.predict(X_test)\n",
    "\n",
    "# 计算准确率\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(\"决策树分类准确率:\", accuracy)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
